﻿/*
 * 
 *  
 *  2023/3/3
 *  1、排序
 *  2、根据商品id获取详情
 *      详情：  1）基本信息和图片
 *              2）产品对应的颜色和尺寸
 * 
 * 
 * **/

using Max.PH5_2008A.ShoppingCode.Domain;
using Max.PH5_2008A.ShoppingCode.IRespository;
using Max.PH5_2008A.ShoppingCode.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2008A.ShoppingCode.Service.Product
{
    public class ProductService : BaseService<TbProductEntity>, IProductService
    {


        IProductRespository _respository;
        IProductImageRespository _imageRespository;
        public ProductService(IProductRespository respositry, IProductImageRespository imageRespository) : base(respositry)
        {
            _respository = respositry;
            _imageRespository = imageRespository;

        }
        public IQueryable<TbProductEntity> Query(int pageIndex, int pageSize, string sortField, string sortValue, out int count)
        {
            var data = _respository.Query();


            switch (sortField.ToLower())
            {
                //根据价格排序
                case "price":
                    //价格的降序
                    if (sortValue.ToLower() == "desc")
                    {
                        data = data.OrderByDescending(x => x.SalePrice);
                    }
                    else
                    {
                        data = data.OrderBy(x => x.SalePrice);
                    }
                    break;
                case "comment":
                    //评论数的降序
                    if (sortValue.ToLower() == "desc")
                    {
                        data = data.OrderByDescending(x => x.CommentNumber);
                    }
                    else
                    {
                        data = data.OrderBy(x => x.CommentNumber);
                    }
                    break;
                default:
                    if (sortValue.ToLower() == "desc")
                    {
                        data = data.OrderByDescending(x => x.Id); //id的升序
                    }
                    else
                    {
                        data = data.OrderBy(x => x.Id);//id的降序
                    }
                    break;
            }

            count = data.Count();
            var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }


        public dynamic GetProductDetailInfo(string id)
        {
            //所有信息
            var data = _respository.Query();
            //所有图片信息
            var imgs = _imageRespository.Query();

            //获取一条的
            var model = data.Where(x => id.Equals(x.Id.ToString())).FirstOrDefault();

            //颜色
            var color = from a in data
                    where a.SPUId == model.SPUId
                    group a by a.SpecColor into c
                    select new
                    {
                        c.Key
                    };

            //尺寸
            var size = from a in data
                         where a.SPUId == model.SPUId
                         group a by a.SpecSize into c
                         select new
                         {
                             c.Key
                         };

            //具体的数据带图片
            var list1 = from a in data
                        where a.Id.ToString() == id
                        select new
                        {
                            a.Id,
                            a.Name,
                            a.SalePrice,
                            a.SpecColor,
                            a.SpecSize,
                            a.ValidStock,
                            images = imgs.Where(x => x.ProductId == a.Id).ToList()
                        };
            //var list2 = from a in data
            //            join b in imgs on a.Id equals b.ProductId
            //            where a.Id.ToString() == id
            //            select new
            //            {
            //                a.Id,
            //                a.Name,
            //                a.SalePrice,
            //                a.SpecColor,
            //                a.SpecSize,
            //                a.ValidStock,
            //                b.ImagePath
            //            };

            return new { items = list1,color = color,size = size };
        }
    }
}
